package aor2.cpu.blockoperation;

import aor2.components.AlushiftCircuit;
import aor2.components.Element;
import aor2.components.ElementIO;
import aor2.components.Register;
import aor2.components.Restorable;
import aor2.cpu.InternBus;
import aor2.cpu.blockcontrol.ControlSignalSet;

public class AlushiftXYM1 extends AlushiftCircuit implements Element,
		ElementIO, Restorable {

	private static AlushiftXYM1 singleton = null;

	public static AlushiftXYM1 getInstance() {
		if (singleton == null)
			singleton = new AlushiftXYM1();
		return singleton;
	}

	private Register x, y;

	private AlushiftXYM1() {
		X = x = new Register(0, InternBus.M2, null);
		Y = y = new Register(0, InternBus.M2, null);
		out = InternBus.M1;
		ControlSignalSet css = ControlSignalSet.getInstance();
		css.get("ALUSHIFTout").addSourceElementIO(this);
		css.get("Xin").addDestinationElementIO(x);
		css.get("Yin").addDestinationElementIO(y);
	}

	public void inX() {
		x.in();
	}

	public void inY() {
		y.in();
	}

	public Register x() {
		return x;
	}

	public Register y() {
		return y;
	}

	@Override
	public long getState() {
		return super.getState();
	}

	@Override
	public void setState(long state) {
		// ignore Command
	}

	@Override
	public String toString() {
		return "ALUSHIFT [X=" + x + ", Y=" + y + "]";
	}

	@Override
	public void in() {
		// ignore
	}

	@Override
	public void out() {
		setOperation(AlushiftSignal.getActiveOperation());
		super.out();
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		String[] states = trim.split(" ");

		x.setState(Long.parseLong(states[0]));
		y.setState(Long.parseLong(states[1]));

	}

	@Override
	public String createRestorePoint() {
		return beginTag() + "\n\t" + x.getState() + " " + y.getState() + " "
				+ "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<ALUXYZ>";
	}

	@Override
	public String endTag() {
		return "</ALUXYZ>";
	}
}
